package com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.yadiudiu.house.MyAdapter.HousePurchaseFragmentAdapter;
import com.diudiutechnology.www.yadiudiu.util.utils.DisplayMetricsConverter;
import com.diudiutechnology.www.yadiudiu.util.utils.SharedPreferencesUtil;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.BaseActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.BuyACarFragmentModel;
import com.diudiutechnology.www.yadiudiu.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/15.
 * 搜索
 */

public class SearchActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.title_center)
    TextView titleCenter;
    @Bind(R.id.retur)
    LinearLayout retur;
    @Bind(R.id.task_search)
    EditText taskSearch;
    @Bind(R.id.contentLayout)
    LinearLayout contentLayout;
    @Bind(R.id.determine)
    TextView determine;
    @Bind(R.id.search_list)
    ListView search_list;

    @Bind(R.id.searchhistory)
    LinearLayout searchhistory;

    private List<BuyACarFragmentModel> houselist;
    HousePurchaseFragmentAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);

        initView();
    }
private  void initView(){
    retur.setOnClickListener(this);
    determine.setOnClickListener(this);
    show();
    search_list.setVisibility(View.GONE);
    adapter=new HousePurchaseFragmentAdapter(this);
    search_list.setAdapter(adapter);
    houselist=new ArrayList<>();

    initData();
}
    private void initData() {


        houselist.clear();
        BuyACarFragmentModel model=new BuyACarFragmentModel();
        model.setName("渝北 万科朗润园 精装单间 家电齐全 拎包入住");
        model.setLet("农业园区 万科朗润园");
        model.setMoney1("1,000元/月");
        model.setDistance("0.62km");
        model.setDate("1室1厅  整租");
        houselist.add(model);



        model=new BuyACarFragmentModel();
        model.setName("渝北 万科朗润园 精装单间 家电齐全 拎包入住");
        model.setLet("农业园区 万科朗润园");
        model.setMoney1("1,000元/月");
        model.setDistance("1.12km");
        model.setDate("1室1厅  整租");
        houselist.add(model);

        model.setName("渝北 万科朗润园 精装单间 家电齐全 拎包入住");
        model.setLet("农业园区 万科朗润园");
        model.setMoney1("950元/月");
        model.setDistance("1.12km");
        model.setDate("1室1厅  合租");
        houselist.add(model);
        adapter.addItem(houselist,1);
    }

    /**
     * 获取历史记录
     */
    private void show() {
        String historyString = SharedPreferencesUtil.getString(SharedPreferencesUtil.Keys.SEARCH_KEYS, "");
        if (Sundry.isEmpty(historyString)) {
            contentLayout.setVisibility(View.GONE);
        } else {
            contentLayout.setVisibility(View.VISIBLE);
        }
        System.out.println("customLayout"+historyString);
        contentLayout.removeAllViews();
        String[] arr = historyString.split("&");

        for (int i = 0; i < arr.length; i++) {
            final TextView tv = new TextView(this);
            tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));
            tv.setSingleLine();
            tv.setTextColor(Color.parseColor("#4d4d4d"));
            tv.setTextSize(14);
            tv.setPadding(DisplayMetricsConverter.DipToPixel(this, 15), DisplayMetricsConverter.DipToPixel(this, 10),
                    DisplayMetricsConverter.DipToPixel(this, 15), DisplayMetricsConverter.DipToPixel(this, 10));
            tv.setBackgroundColor(Color.parseColor("#ffffff"));
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tv.getLayoutParams();
            lp.setMargins(0, 0, 0, DisplayMetricsConverter.DipToPixel(this, 1));
            tv.setText(arr[i]);
            tv.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    taskSearch.setText(tv.getText().toString().trim());
                     search();
                }
            });

            System.out.println("customLayout"+tv.getText());
            contentLayout.addView(tv);
        }

    }

    /**
     * 搜索
     */
    private void search() {
        String key = taskSearch.getText().toString().trim();
        if (Sundry.isEmpty(key)) {
            ToastUtils.show(this, "请输入您要搜索的商品名");
            return;
        }
        save();
        if (key.equals("渝北")){
            search_list.setVisibility(View.VISIBLE);
            searchhistory.setVisibility(View.GONE);
        }else {
            search_list.setVisibility(View.GONE);
            searchhistory.setVisibility(View.VISIBLE);
            show();
        }

    }
    //缓存
    private void save() {
        String content = taskSearch.getText().toString().trim();
        String oldString = SharedPreferencesUtil.getString(SharedPreferencesUtil.Keys.SEARCH_KEYS, "");
        String[] arr = oldString.split("&");
        List<String> listArr = Arrays.asList(arr);
        if (!listArr.contains(content)) {
            SharedPreferencesUtil.putString(SharedPreferencesUtil.Keys.SEARCH_KEYS, oldString + content + "&");
        }
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.determine:
                search();
                break;
        }
    }
}
